<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-03-27 22:24:58
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denied');
}
class TouchContrl extends BaseXf
{
    public $pagesize = 30;
    public function index()
    {
        global $_G;
        $paihang         = $this->_paihang();
        $catedata        = $this->_tuijian();
        $flash           = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(6);
        $zhuanti         = C::t('#jameson_read#jamesonread_topics')->fetch_books();
        $indexadv        = $this->indexadv();
        $dibunav         = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        $shoujicelannav  = $this->celan();
        $noheaderbar     = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        $navtitle        = preg_replace('/\[zhanming\]/i', $_G['setting']['bbname'], C::t('common_setting')->fetch('xf512seo_index'));
        $metadescription = $_G['jameson_read']['seodescr'];
        $metakeywords    = $_G['jameson_read']['seokeywords'];
        $historys        = $this->history('jamesonreadbooks');
        include template("jameson_read:touch/index");
    }
    public function m()
    {
        $this->index();
    }
    public function paihang()
    {
        global $_G;
        $paihangbang     = $this->_paihang();
        $shoujicelannav  = $this->celan();
        $indexadv        = $this->indexadv();
        $navtitle        = lang('plugin/jameson_read', 'paihangbang') . '-' . $_G['setting']['bbname'];
        $dibunav         = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        $noheaderbar     = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        $metadescription = $_G['jameson_read']['seodescr'];
        $metakeywords    = $_G['jameson_read']['seokeywords'];
        include template("jameson_read:touch/paihang");
    }
    public function fenlei()
    {
        global $_G;
        $navtitle        = lang('plugin/jameson_read', 'quanbufenlei') . '-' . $_G['setting']['bbname'];
        $dibunav         = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        $categorys       = $this->_tuijian();
        $shoujicelannav  = $this->celan();
        $noheaderbar     = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        $metadescription = $_G['jameson_read']['seodescr'];
        $metakeywords    = $_G['jameson_read']['seokeywords'];
        include template("jameson_read:touch/fenlei");
    }

    // 账户
    public function zhanghu()
    {
        global $_G;
        if ($_G['uid']) {
            $navtitle = $_G['member']['username'];
        } else {
            $navtitle = lang('plugin/jameson_read', 'zhanghuzhongxin');
        }
        $navtitle .= '-' . $_G['setting']['bbname'];
        $favores     = C::t('#jameson_read#jamesonread_favores')->fetch_by_page($_G['uid'], 0, C::t('#jameson_read#jamesonread_favores')->count_by_uid($_G['uid']));
        $dashangs    = C::t('#jameson_read#jamesonread_dashang')->fetch_by_uid($_G['uid'], 0, C::t('#jameson_read#jamesonread_dashang')->count_by_uid($_G['uid']));
        $hasbuybooks = C::t('#jameson_read#jamesonread_buybooks')->fetch_books_by_uid($_G['uid']);
        $hasbuytids  = C::t('#jameson_read#jamesonread_buytids')->fetch_tids_by_uid($_G['uid']);
        if ($_G['uid']) {
            $mybookmarks = C::t('#jameson_read#jamesonread_bookmarks')->fetch_by_uid($_G['uid']);
            $myshupiao   = C::t('#jameson_read#jamesonread_piao')->fetch_by_uid($_G['uid'], 1, 0, 30);
        } else {
            $mybookmarks = $myshupiao = array();
        }
        $dibunav         = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        $shoujicelannav  = $this->celan();
        $noheaderbar     = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        $metadescription = $_G['jameson_read']['seodescr'];
        $metakeywords    = $_G['jameson_read']['seokeywords'];
        include template("jameson_read:touch/zhanghu");
    }
    public function category()
    {
        global $_G;
        $category_id   = intval($_GET['category_id']);
        $thecate       = C::t('#jameson_read#jamesonread_categorys')->fetch($category_id);
        $category_name = $thecate['category_name'];
        $categorys     = $this->_tuijian();
        // 类型
        $type = $_GET['type'] ? trim($_GET['type']) : 'zuixin';
        require_once './source/plugin/jameson_read/class/Page.class.php';
        $counts   = C::t('#jameson_read#jamesonread_books')->count_by_con(array('category_id', '=', $category_id));
        $page     = new Page($counts, $this->pagesize, './plugin.php?id=jameson_read&contrl=touch&act=category&category_id=' . $category_id . '&type=' . $type);
        $size     = $this->pagesize;
        $pages    = ceil($counts / $size);
        $pageshow = $page->showweui();
        $books    = C::t('#jameson_read#jamesonread_books')->fetchAll_by_field(
            array(), array(0, $category_id), array(' addtime DESC '), $page->getStart(), $this->pagesize
        );
        $nobbname       = true;
        $navtitle       = preg_replace(array('/\[cate\]/', '/\[zhanming\]/i'), array($category_name, $_G['setting']['bbname']), C::t('common_setting')->fetch('xf512seo_cate'));
        $shoujicelannav = $this->celan();
        $indexadv       = $this->indexadv();
        $dibunav        = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        $noheaderbar    = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        include template("jameson_read:touch/category");
    }
    public function book()
    {
        global $_G;
        $book_id   = intval($_GET['book_id']);
        $hasfavore = C::t('#jameson_read#jamesonread_favores')->hasFavore($_G['uid'], $book_id);
        $books     = C::t('#jameson_read#jamesonread_books')->fetchAllInfoById($book_id, $_G['uid']);
        if (!$books['status']) {
            showmessage(lang('plugin/jameson_read', 'gaishuyixiajia'));
        }
        $books['hasbuy']    = DB::result_first('SELECT count(*) FROM %t WHERE book_id=%d AND buy_id=%d', array('jamesonread_buybooks', $books['book_id'], $_G['uid']));
        $books['dspaihang'] = C::t('#jameson_read#jamesonread_dashang')->fetch_paihang($books['book_id'], 10);
        if ($books['is_pdf']) {
            $books['pdf'] = DB::fetch_all('SELECT * FROM %t WHERE book_id=%d ORDER BY ordernum ASC,addtime ASC', array('jamesonread_pdf', $book_id));
        } else {
            $books['revfenjuan'] = array_reverse($books['fenjuan'], true);
            foreach ($books['revfenjuan'] as $key => $value) {
                $books['revfenjuan'][$key]['colums'] = array_reverse($value['colums'], true);
            }
        }
        if (trim($books['shuxing'])) {
            $books['shuxing'] = explode('|', trim($books['shuxing']));
        }
        // 获取作者信息
        if (trim($books['author']) && $authors_booknum = C::t('#jameson_read#jamesonread_books')->count_by_author($books['author'])) {
            $otherbooks = C::t('#jameson_read#jamesonread_books')->fetch_by_author($books['author'], 0, 4);
        }
        $books['parent_name'] = C::t('#jameson_read#jamesonread_categorys')->fetch_name($books['parent_id']);
        $navtitle = preg_replace(array('/\[fufenlei\]/i', '/\[zifenlei\]/i', '/\[shuming\]/i', '/\[zhanming\]/i'), array($books['parent_name'], $books['category_name'], $books['book_name'], $_G['setting']['bbname']), C::t('common_setting')->fetch('xf512seo_book'));
        // 猜你喜欢
        $hotshuping = C::t('#jameson_read#jamesonread_dianping')->fetch_hot($book_id);
        $appurl     = C::t('common_setting')->fetch('jamesonreadappurl');
        $appurl     = $appurl ? $appurl : '';
        $agent      = strtolower($_SERVER['HTTP_USER_AGENT']);
        $is_iphone  = stripos($agent, 'iphone') ? true : false;
        $is_ipad    = stripos($agent, 'ipad') ? true : false;
        $is_android = stripos($agent, 'android') ? true : false;
        $is_weixin  = $this->is_weixin();
        $ios        = false;
        if ($is_iphone || $is_ipad) {
            $ios = true;
        }
        $piaoinfo = DB::fetch_all('SELECT * FROM %t WHERE ' . DB::field('type', array(101, 102, 103), 'in'), array('jamesonread_topics'), 'type');
        if ($_G['uid']) {
            $dquid = C::t('common_member_count')->fetch($_G['uid']);
        } else {
            $dquid = array();
        }
        // 判断阅读权限
        $isread = array(
            'is'  => (!$books['bookmoney'] || ($_G['uid'] == $books['uid'])) ? 1 : 0,
            'vip' => (in_array($_G['groupid'], $_G['jameson_read']['vipgroup'])) ? 1 : 0,
            'has' => $books['hasbuy'] ? 1 : 0,
        );
        $fenshu          = $books['dpcount'] ? round(intval($books['scores']) / intval($books['dpcount']), 1) : 0;
        $likesame        = C::t('#jameson_read#jamesonread_books')->fetch_search($books['book_name'], 'same', 0, 7);
        $shoujicelannav  = $this->celan();
        $indexadv        = $this->indexadv();
        $dibunav         = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        $noheaderbar     = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        $metadescription = $books['book_name'] . ',' . $books['desco'] . $_G['jameson_read']['seodescr'];
        $metakeywords    = $books['book_name'] . ',' . $_G['jameson_read']['seokeywords'];
        $historys        = $this->history('jamesonreadbooks');
        include template("jameson_read:touch/book");
    }

    public function mulu()
    {
        global $_G;
        $book_id    = intval($_GET['mulu_id']);
        $book       = C::t('#jameson_read#jamesonread_books')->fetch($book_id);
        $fenjuan    = C::t('#jameson_read#jamesonread_fenjuan')->fetch_all_bybid($book_id);
        $revfenjuan = array_reverse($fenjuan, true);
        foreach ($revfenjuan as $key => $value) {
            $revfenjuan[$key]['zhang'] = array_reverse($value['zhang'], true);
        }
        $shoujicelannav    = $this->celan();
        $navtitle          = $book['book_name'] . '-' . $_G['setting']['bbname'];
        $dibunav           = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        $noheaderbar       = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        $book['hasbuy']    = DB::result_first('SELECT count(*) FROM %t WHERE book_id=%d AND buy_id=%d', array('jamesonread_buybooks', $book['book_id'], $_G['uid']));
        $isread['showbuy'] = !$book['is_pdf'] && !$book['hasbuy'] && !in_array($_G['groupid'], $_G['jameson_read']['vipgroup']) && ($book['uid'] != $_G['uid']) ? 1 : 0;
        $metadescription   = $_G['jameson_read']['seodescr'];
        $metakeywords      = $_G['jameson_read']['seokeywords'];
        include template("jameson_read:touch/mulu");
    }
    public function zuozhe()
    {
        global $_G;
        $author = daddslashes($_GET['author']);
        if (xfxs_isutf($author)) {
            if (strtolower(CHARSET) != 'utf-8') {
                $author = diconv($author, 'utf-8', CHARSET);
            }
        } else if ('utf-8' == strtolower(CHARSET)) {
            $author = diconv($author, 'gbk', CHARSET);
        }
        $counts = C::t('#jameson_read#jamesonread_books')->count_by_author($author);
        require_once './source/plugin/jameson_read/class/Page.class.php';
        $page            = new Page($counts, $this->pagesize, './plugin.php?id=jameson_read&contrl=touch&act=zuozhe&author=' . $author);
        $books           = C::t('#jameson_read#jamesonread_books')->fetch_by_author($author, $page->getStart(), $page->getSize());
        $pageshow        = $page->showweui();
        $shoujicelannav  = $this->celan();
        $indexadv        = $this->indexadv();
        $navtitle        = $author . '-' . $_G['setting']['bbname'];
        $dibunav         = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        $metadescription = $_G['jameson_read']['seodescr'];
        $metakeywords    = $_G['jameson_read']['seokeywords'];
        $noheaderbar     = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        include template("jameson_read:touch/zuozhe");
    }
    public function shuping()
    {
        global $_G;
        $book_id = intval($_GET['book_id']);
        $book    = C::t('#jameson_read#jamesonread_books')->fetch($book_id);
        $counts  = C::t('#jameson_read#jamesonread_dianping')->count_by_bid($book_id);
        require_once './source/plugin/jameson_read/class/Page.class.php';
        $page            = new Page($counts, $this->pagesize, './plugin.php?id=jameson_read&contrl=touch&act=shuping&book_id=' . $book_id);
        $pageshow        = $page->showweui();
        $shuping         = C::t('#jameson_read#jamesonread_dianping')->fetch_new($book_id, $page->getStart(), $page->getSize());
        $shoujicelannav  = $this->celan();
        $indexadv        = $this->indexadv();
        $navtitle        = $book['book_name'] . '-' . $_G['setting']['bbname'];
        $dibunav         = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        $noheaderbar     = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        $metadescription = $_G['jameson_read']['seodescr'];
        $metakeywords    = $_G['jameson_read']['seokeywords'];
        include template("jameson_read:touch/shuping");
    }
    // 我的书架
    public function store()
    {
        global $_G;
        $navtitle = lang('plugin/jameson_read', 'wodeshujia') . '-' . $_G['setting']['bbname'];
        // 阅读记录
        $historys = $this->history('jamesonreadbooks');
        // 书架推荐好书
        $shujiahaoshu = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(13);
        if ($_G['uid']) {
            $shoucang = C::t('#jameson_read#jamesonread_favores')->fetch_by_page($_G['uid'], 0, 0);
            if ($shoucang) {
                $bookids = array();
                foreach ($shoucang as $key => $value) {
                    $bookids[] = $value['book_id'];
                }
            }
            $shuqian = C::t('#jameson_read#jamesonread_bookmarks')->fetch_by_uid($_G['uid'], 1);
        }
        $morentj = array();
        if (trim($shujiahaoshu[0]['adv'], ',')) {
            $book_ids = explode(',', trim($shujiahaoshu[0]['adv'], ','));
            foreach ($book_ids as $key => $value) {
                if (intval($value)) {
                    $morentj[] = C::t("#jameson_read#jamesonread_books")->fetch(intval($value));
                }
            }
        }
        $shoujicelannav = $this->celan();
        $indexadv       = $this->indexadv();
        // 手机底部导航 type=120
        $dibunav         = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        $noheaderbar     = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        $metadescription = $_G['jameson_read']['seodescr'];
        $metakeywords    = $_G['jameson_read']['seokeywords'];
        include template('jameson_read:touch/store');
    }
    // 搜索
    public function search()
    {
        global $_G;
        $keyword = dhtmlspecialchars(trim($_GET['keyword']));
        if (xfxs_isutf($keyword)) {
            if (strtolower(CHARSET) != 'utf-8') {
                $keyword = diconv($keyword, 'utf-8', CHARSET);
            }
        } else if ('utf-8' == strtolower(CHARSET)) {
            $keyword = diconv($keyword, 'gbk', CHARSET);
        }
        $keyword  = urldecode($keyword);
        $navtitle = !$keyword ? lang('plugin/jameson_read', 'sousuo') : $keyword . '-' . lang('plugin/jameson_read', 'sousuojieguo') . '-' . $_G['setting']['bbname'];
        $books    = array();
        if ($keyword) {
            $counts = C::t('#jameson_read#jamesonread_books')->count_search($keyword, 'book');
            if ($counts) {
                if (!isset($_GET['p'])) {
                    C::t('#jameson_read#jamesonread_search')->addkeyword($keyword);
                }
                require_once './source/plugin/jameson_read/class/Page.class.php';
                $page     = new Page($counts, 20, './php?id=jameson_read&contrl=touch&act=search&keyword=' . $keyword);
                $books    = C::t('#jameson_read#jamesonread_books')->fetch_search($keyword, 'book', $page->getStart(), $page->getSize());
                $pageshow = $page->showweui();
            }
        } else {
            $hotkey = C::t('#jameson_read#jamesonread_search')->fetch_by_counts(10);
        }
        $shoujicelannav = $this->celan();
        $indexadv       = $this->indexadv();
        // 手机底部导航 type=120
        $dibunav         = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        $noheaderbar     = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        $metadescription = $_G['jameson_read']['seodescr'];
        $metakeywords    = $_G['jameson_read']['seokeywords'];
        include template('jameson_read:touch/search');
    }
    public function tag()
    {
        global $_G;
        $tag = dhtmlspecialchars(trim($_GET['tag']));
        if (xfxs_isutf($tag) && strtolower(CHARSET) != 'utf-8') {
            $tag = diconv($tag, 'utf-8', CHARSET);
        } else if (!xfxs_isutf($tag) && strtolower($tag) == 'utf-8') {
            $tag = diconv($tag, 'GB2312', CHARSET);
        }
        $navtitle = !$tag ? lang('plugin/jameson_read', 'sousuotushu') : $tag . lang('plugin/jameson_read', 'sousuojieguo') . '-' . $_G['setting']['bbname'];
        if ($tag) {
            if (!isset($_GET['p'])) {
                C::t('#jameson_read#jamesonread_search')->addkeyword($tag, 'tag');
            }
            $currentpage = $_GET['p'] ? intval($_GET['p']) : 1;
            $counts      = C::t('#jameson_read#jamesonread_books')->count_search($tag, 'shuxing');
            if ($counts) {
                require_once './source/plugin/jameson_read/class/Page.class.php';
                $page     = new Page($counts, 20, './plugin.php?id=jameson_read&contrl=touch&act=tag&tag=' . $tag);
                $books    = C::t('#jameson_read#jamesonread_books')->fetch_search($tag, 'shuxing', ($currentpage - 1) * 30, 30);
                $pageshow = $page->showweui();
            }
        } else {
            $hottag = C::t('#jameson_read#jamesonread_search')->fetch_tag_paihang();
        }
        $shoujicelannav = $this->celan();
        $indexadv       = $this->indexadv();
        // 手机底部导航 type=120
        $dibunav         = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        $noheaderbar     = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        $metadescription = $_G['jameson_read']['seodescr'];
        $metakeywords    = $_G['jameson_read']['seokeywords'];
        include template('jameson_read:touch/tag');
    }

    // 首页 精品推荐 最新上架 畅销 查看更多
    public function more()
    {
        global $_G;
        $type  = daddslashes($_GET['type']);
        $order = $_GET['order'] ? daddslashes($_GET['order']) : 'views';
        require_once './source/plugin/jameson_read/class/Page.class.php';
        $byorder  = 'addtime desc';
        $navtitle = lang('plugin/jameson_read', $type . 'books');
        if ($type == 'is_jingxuan' || $type == 'is_top') {
            $con = array('is_top', '=', 1);
        } else if ($type == 'addtime') {
            $con = array();
        } else if ($type == 'sell') {
            $con = array('sell', 0, 0);
        } else if ($type == 'mianfei') {
            $con = array('bookmoney', '=', 0);
        } else if ($type == 'vip') {
            $con     = array('bookmoney', '>', 0);
            $byorder = 'bookmoney asc';
        } else {
            switch ($order) {
                case 'views':
                case 'favores':
                case 'scores':
                    $con     = array();
                    $byorder = $order . ' desc';
                    break;
                case 'end':
                    $con = array('plan', '>', 0);
                    break;
                case 'sell':
                    $con = array('sell', 0, 0);
                    break;
                case 'xinren':
                    $con = array();
                    break;
                case 'update':
                    $con = array();
                    break;
            }
            $navtitle = lang('plugin/jameson_read', $order . 'books');
        }
        if (isset($order) && $order == 'xinren') {
            $books = C::t('#jameson_read#jamesonread_books')->fetch_by_newauthor(0, $this->pagesize);
        } else if (isset($order) && $order == 'update') {
            $books = C::t('#jameson_read#jamesonread_update')->fetch_update(30);
        } else {
            $count    = C::t('#jameson_read#jamesonread_books')->count_by_con($con);
            $page     = new Page($count, $this->pagesize, './plugin.php?id=jameson_read&contrl=touch&act=more&type=' . $type);
            $pageshow = $page->showweui();
            $books    = C::t('#jameson_read#jamesonread_books')->fetchAll_by_field($con, array(), array($byorder), $page->getStart(), $page->getSize());
        }
        $shoujicelannav = $this->celan();
        $indexadv       = $this->indexadv();
        // 手机底部导航 type=120
        $dibunav = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(120);
        $title   = $navtitle;
        $navtitle .= '-' . $_G['setting']['bbname'];
        $noheaderbar     = (int) C::t('common_setting')->fetch('xf512read_noheaderbar');
        $metadescription = $_G['jameson_read']['seodescr'];
        $metakeywords    = $_G['jameson_read']['seokeywords'];
        include template('jameson_read:touch/more');
    }
    public function read()
    {
        global $_G;
        $columinfo = C::t('#jameson_read#jamesonread_colums')->fetch_colum_bytid($_GET['tid']); //获取章节信息
        if (!$columinfo) {
            // 不存在章节 if--1 end
            showmessage(lang('plugin/jameson_read', 'zhangjiebucunzaihuoyishanchu'), '', 'error');
        }
        // 如果审核中
        if (DB::result_first('SELECT count(*) FROM %t WHERE id=%d', array('forum_thread_moderate', $_GET['tid']))) {
            $backurl = dreferer();
            // 如果来源是发帖页面，则跳转到作者中心
            if (preg_match("/mod=post/i", $backurl)) {
                $backurl = './plugin.php?id=jameson_read&contrl=author&act=book&book_id=' . $columinfo['book_id'] . '&forward=book_column';
            }
            showmessage(lang('plugin/jameson_read', 'shenhezhong'), $backurl);
        }
        // 图书信息
        $bookinfo = C::t('#jameson_read#jamesonread_books')->fetchAllInfoById($columinfo['book_id']);
        // 如果本书具有售价则判断是否购买了本书
        if ($columinfo['price'] && C::t('#jameson_read#jamesonread_buybooks')->hasBuyBook($_G['uid'], $bookinfo['book_id'])) {
            $shifougoumaibutton = 1;
        } else {
            $shifougoumaibutton = 0;
        }
        // 是否购买了本章节
        if (!$columinfo['price'] || ($columinfo['uid'] == $_G['uid']) || in_array($_G['groupid'], $_G['jameson_read']['vipgroup']) || C::t('#jameson_read#jamesonread_buytids')->hasBuyTid($_G['uid'], $columinfo['tid'])) {
            $shifougoumaizhangjie = 1;
        } else {
            $shifougoumaizhangjie = 0;
        }
        if ($shifougoumaibutton < 1 && $shifougoumaizhangjie < 1) {
            // 进入这里，本章节需要购买才可阅读
            if (getcookie('jamesonreadautobuy_' . $_G['uid'])) {
                // 如果设置了自动购买，则自动扣费，否则显示购买按钮
                if ($this->autobuy($columinfo)) {
                    $shifougoumaizhangjie = 1;
                }
            }
        }
        if (($_G['jameson_read']['mustlogin'] && !$_G['uid']) || ($shifougoumaibutton < 1 && $shifougoumaizhangjie < 1)) {
            // 如果没有购买过，则进行提示购买操作
            // 首先获取本章节的前200个文字
            $message = C::t("forum_post")->fetch('tid:' . $columinfo['tid'], $columinfo['pid']);
            $message = cutstr($message['message'], 500, '');
            $message = preg_replace(array('/\[attach\].*?\[\/attach\]/ism', "/\[\w+[^\]]\]/ism", "/\[\/\w+\]/ism", "/[^\w_=\/]jameson_read/ism"), array('', '', "", ""), $message);
            $message = cutstr(trim(strip_tags($message)), 200);
            if ($shifougoumaibutton < 1 && $shifougoumaizhangjie < 1) {
                $shifoukedu = 0;
                $message .= '<strong class="xs3 marlm text-danger">'
                . lang('plugin/jameson_read', 'goumaibenzhangjiejixu')
                    . '</strong>';
            } else {
                $shifoukedu = 1;
                $message .= '<a href="member.php?mod=logging&action=login" class="xs3 marlm text-danger">'
                . lang('plugin/jameson_read', 'dengluhouyueduwanzheng')
                    . '</a>';
            }
        } else {
            $shifoukedu = 1;
            // {
            $nodisplayauthor = 0;
            $message         = C::t("forum_post")->fetch('tid:' . $columinfo['tid'], $columinfo['pid']);
            if (preg_match("/jameson_read\s*$/", $message['message'])) {
                // 读取图片附件
                $_G['forum_attachpids'][] = $columinfo['pid'];
                include_once './source/function/function_discuzcode.php';
                if (preg_match_all("/\[attach\](\d+)\[\/attach\]/ism", $message['message'], $matchaids)) {
                    $_G['forum_attachtags'][$columinfo['pid']] = $matchaids[1];
                }
                $tupianfujian = array();
                $_G['tid']    = $columinfo['tid'];
                require_once libfile('function/attachment');
                parseattach($_G['forum_attachpids'], $_G['forum_attachtags'], $tupianfujian);
                if ($tupianfujian[$columinfo['pid']]['attachments']) {
                    foreach ($tupianfujian[$columinfo['pid']]['attachments'] as $key => $value) {
                        if (in_array(strtolower($value['ext']), array('jpg', 'png', 'gif'))) {
                            $img = '<img src="'
                                . $value['url']
                                . $value['attachment']
                                . '" />';
                            $patt = "/\[attach\]("
                                . $key
                                . ")\[\/attach\]/ism";
                            $message['message'] = preg_replace($patt, $img, $message['message']);
                        }
                    }
                }
                // 音频文件处理
                $message['message'] = preg_replace(array("/\[audio\](.*?)\[\/audio\]/", "|\[img=?(\d+)?,?(\d+)?\](.*?)\[/img\]|"), array('<audio src="$1"  controls="controls"></audio>', '<img src="$3" width="$1" height="$2">'), $message['message']);
                // 原始解析后
                $codemessage = discuzcode($message['message'], 0, 0, 1, 1, 1, 1, 1, 0, '0', $columinfo['uid'], '0');

                $message = preg_replace(array(
                    "/[^=_\/]jameson_read/ism",
                    "/\[code\](.*?)\[\/code\]/ism",
                    "/\[quote\]/ism",
                    "/\[\/quote\]/ism",
                    "/&#\d+;/ism",
                ),
                    array(
                        '',
                        '######',
                        '<blockquote>',
                        '</blockquote>',
                        '',
                    ), $codemessage);

                preg_match_all('/\[code\](.*?)\[\/code\]/ism', $codemessage, $codehtml);
                // 存在源代码格式
                if ($codehtml) {
                    $codearray = array();
                    foreach ($codehtml[1] as $key => $value) {
                        $tmp = '<pre>'
                        . dhtmlspecialchars($value)
                            . '</pre>';
                        $message = preg_replace('/######/', $tmp, $message);
                    }
                }
                // cuplayer视频解析
                if (preg_match("/\[cuplayer\](.*)\[\/cuplayer\]/", $message, $cuplayer)) {
                    $message = preg_replace("/\[cuplayer\](.*)\[\/cuplayer\]/", '<video  src="{$cuplayer[1]}" width="100%" controls preload></video>', $message);
                }
                if (!preg_match("/<(p|div)[^>]*?>/ism", $message)) {
                    $message = '<p>' . preg_replace(array(
                        "/\n/s",
                        "/<br\s*?\/?>/ism",
                    ), array(
                        "</p><p>",
                        "</p><p>",
                    ), $message) . '</p>';
                }
                $message = preg_replace(array(
                    "/<p\s*?>\s*?<br\s*\/?>/ism",
                ), array(
                    "<p>",
                ), $message);
                $message = stripcslashes($message);
            } else {
                $aftermessage = preg_replace(
                    array(
                        "/<code>(.*?)<\/code>/ism",
                    ),
                    array(
                        '######',
                    ), $message['message']);
                $aftermessage = preg_replace(
                    array(
                        "/(<\w+[^>]*?\s)on(\w+)/ism",
                    ),
                    array(
                        '\1 \2',
                    ), $aftermessage);
                preg_match_all('/<code>(.*?)<\/code>/ism', $message['message'], $codehtml2);
                // 存在源代码格式
                if ($codehtml2) {
                    $codearray = array();
                    foreach ($codehtml2[1] as $key => $value) {
                        $tmp = '<code>'
                        . preg_replace(array('/</', '/>/'), array('&lt;', '&gt;'), $value)
                            . '</code>';
                        $aftermessage = preg_replace('/######/', $tmp, $aftermessage, 1);
                    }
                }
                $message = stripcslashes($aftermessage);
            }
            $subject = $columinfo['subject'];
            if(isset($_GET['highlight']) && $_GET['highlight']){
                require_once libfile('function/search');
                $subject = bat_highlight($columinfo['subject'],$_GET['highlight']);
                $message = bat_highlight($message,$_GET['highlight']);
            }
            if ($_G['uid'] && ($_GET['fxuid'] > 0) && !in_array($_G['uid'], array($_GET['fxuid'], $columinfo['uid']))) {
                // 分享奖励积分
                $this->fxact($_GET['fxuid'], $columinfo['tid'], $columinfo['book_id'], $_G['jameson_read']['jiangli'], $_G['jameson_read']['fxtrameid'], $_G['clientip']);
            }
            // 添加阅读量数据
            C::t('#jameson_read#jamesonread_books')->addviews($columinfo['book_id']);
            // 插入阅读历史记录
            if ($history = getcookie('jamesonreadbooks')) {
                $history     = unserialize($history);
                $hisflag     = false;
                $lasthasread = '';
                foreach ($history as $key => $value) {
                    $tmp = explode(',', $value);
                    if ($tmp[0] == $columinfo['book_id']) {
                        if ($hisflag) {
                            unset($history[$key]);
                            continue;
                        }
                        // 存在相同book_id更新
                        if ($tmp[1] != $columinfo['tid']) {
                            $history[$key] = $columinfo['book_id']
                                . ','
                                . $columinfo['tid']
                                . ','
                                . $columinfo['pid']
                                . ','
                                . $columinfo['subject']
                                . ','
                                . $bookinfo['book_name']
                                . ','
                                . $bookinfo['image'];
                            $lasthasread = $value;
                        }
                        $hisflag = true;
                    }
                }
                if (!$hisflag) {
                    $history[] = $columinfo['book_id']
                        . ','
                        . $columinfo['tid']
                        . ','
                        . $columinfo['pid']
                        . ','
                        . $columinfo['subject']
                        . ','
                        . $bookinfo['book_name']
                        . ','
                        . $bookinfo['image'];
                }
            } else {
                $history = array($columinfo['book_id']
                    . ','
                    . $columinfo['tid']
                    . ','
                    . $columinfo['pid']
                    . ','
                    . $columinfo['subject']
                    . ','
                    . $bookinfo['book_name']
                    . ','
                    . $bookinfo['image']);
            }
            dsetcookie('jamesonreadbooks', serialize($history), 240 * 3600);
        }

        $piaoinfo = DB::fetch_all('SELECT * FROM %t WHERE ' . DB::field('type', array(101, 102, 103), 'in'), array('jamesonread_topics'), 'type');
        if ($_G['uid']) {
            $dquid = C::t('common_member_count')->fetch($_G['uid']);
        } else {
            $dquid = array();
        }
        // 上一章数据
        $predata = C::t('#jameson_read#jamesonread_colums')->precolum($columinfo['book_id'], $columinfo['zhangjie'], $columinfo['colum_id']);
        // 下一章数据
        $nextdata = C::t('#jameson_read#jamesonread_colums')->nextcolum($columinfo['book_id'], $columinfo['zhangjie'], $columinfo['colum_id']);
        // adv
        if ($_G['jameson_read']['pcyuedugg']) {
            $pcyuedugg = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(7);
        }
        // 不论是否可读都将显示的通用信息
        // 分卷信息
        $revmuluarray = array_reverse($bookinfo['fenjuan'], true);
        foreach ($revmuluarray as $key => $value) {
            $revmuluarray[$key]['colums'] = array_reverse($value['colums'], true);
        }
        // 本书是否在书架
        $hasfavore = C::t('#jameson_read#jamesonread_favores')->hasFavore($_G['uid'], $columinfo['book_id']);
        // 本章节是否已加入书签
        $hasmark = !$_G['uid'] ? 0 : C::t('#jameson_read#jamesonread_bookmarks')->hasMark($columinfo['tid'], $_G['uid']);
        // 本书的所有书签信息
        $marks = C::t('#jameson_read#jamesonread_bookmarks')->fetch_bybid($_G['uid'], $columinfo['book_id']);
        // 取得本书的书评信息
        $shuping = C::t('#jameson_read#jamesonread_dianping')->fetch_hot($columinfo['book_id']);
        // 评分
        $scores = round($bookinfo['scores'] / $bookinfo['dpcount'], 1);
        // 图书封面图片处理
        if ($bookinfo['image']) {
            $image = './data/attachment/forum/' . $bookinfo['image'];
        } else {
            $image = $_G['jameson_read']['morenfengmian'];
        }
        $bookinfo['parent_name'] = C::t('#jameson_read#jamesonread_categorys')->fetch_name($bookinfo['parent_id']);
        $nobbname = true;
        $navtitle = preg_replace(array('/\[fufenlei\]/i', '/\[zifenlei\]/i', '/\[zhangjie\]/i', '/\[shuming\]/i', '/\[zhanming\]/i'), array($bookinfo['parent_name'], $bookinfo['category_name'], $columinfo['subject'], $bookinfo['book_name'], $_G['setting']['bbname']), C::t('common_setting')->fetch('xf512seo_read'));
        $_G['setting']['seohead'] .= '<meta name="renderer"content="webkit" />';
        $metadescription = $columinfo['subject'] . ',' . $bookinfo['book_name'] . ',' . $bookinfo['desco'] . $_G['jameson_read']['seodescr'];
        $metakeywords    = $columinfo['subject'] . ',' . $bookinfo['book_name'] . ',' . $_G['jameson_read']['seokeywords'] . ',';
        if ($_G['jameson_read']['shoujiyuedugg']) {
            $shoujiyuedugg = C::t('#jameson_read#jamesonread_topics')->fetch_by_type(8);
        }
        $shoujicelannav = $this->celan();
        $autobuycookie  = 0;
        if ($_G['uid'] && getcookie('jamesonreadautobuy_' . $_G['uid'])) {
            $autobuycookie = 1;
        }
        include template('jameson_read:touch/read');
    }
    public function shenqing()
    {
        global $_G;
        if (!$_G['uid']) {
            showmessage(lang('plugin/jameson_read', 'qingxiandenglu'), 'member.php?mod=logging&action=login', 'error');
        }
        if (FORMHASH == $_GET['formhash']) {
            $data             = array();
            $data['xingming'] = daddslashes(trim($_GET['xingming']));
            $data['lianxi']   = daddslashes(trim($_GET['lianxi']));
            $data['fuyan']    = daddslashes(trim($_GET['fuyan']));
            $data['author']   = daddslashes(trim($_G['username']));
            $data['uid']      = $_G['uid'];
            $data['groupid']  = $_G['groupid'];
            $data['addtime']  = TIMESTAMP;
            C::t('#jameson_read#jamesonread_shenhe')->insert($data);
            showmessage(lang('plugin/jameson_read', 'yitijiao'), dreferer(), 'succeed');
        } else {
            // 是否已有申请
            $hasdata         = DB::fetch_first('SELECT * FROM %t WHERE uid=%d', array('jamesonread_shenhe', $_G['uid']));
            $paihangbang     = $this->_paihang();
            $categorys       = $this->_tuijian();
            $navtitle        = lang('plugin/jameson_read', 'shenqingzuozhe');
            $adv             = $this->indexadv();
            $metadescription = $_G['jameson_read']['seodescr'];
            $metakeywords    = $_G['jameson_read']['seokeywords'];
            $_G['setting']['seohead'] .= '<meta name="renderer"content="webkit" />';
            include template("jameson_read:touch/shenqing");
        }
    }
    protected function celan()
    {
        global $_G;
        return C::t('#jameson_read#jamesonread_topics')->fetch_by_type(10);
    }
    protected function indexadv()
    {
        global $_G;
        return C::t('#jameson_read#jamesonread_topics')->fetch_mb_adv(11, 12);
    }
    public function download()
    {
        global $_G;
        $book_id = intval($_GET['book_id']);
        // 未登录不存在图书或用户组禁止下载
        if (!in_array($_G['groupid'], $_G['jameson_read']['downgroups'])) {
            showmessage(lang('plugin/jameson_read', 'xiazaichucuo', ''));
        }
        // 如果当前图书不存在售价则可免费下载
        $bookinfo = C::t('#jameson_read#jamesonread_books')->fetch($book_id);
        if ($bookinfo['is_pdf'] > 0) {
            showmessage(lang('plugin/jameson_read', 'xiazaichucuo', ''));
        }
        $tmpfile = "******" . $_G['setting']['bbname'] . "(" . $_G['siteurl'] . ")******\r\n<<" . $bookinfo['book_name'] . '>>  [' . $bookinfo['author'] . "]\r\n";
        // 所有章节
        $allcolums = DB::fetch_all('SELECT pid,subject,tid FROM %t WHERE book_id=%d ORDER BY zhangjie', array('jamesonread_colums', $book_id));
        // 处理messag
        foreach ($allcolums as $key => $value) {
            $message = C::t("forum_post")->fetch('tid:' . $value['tid'], $value['pid']);
            $message = $message['message'];
            $tmp     = preg_replace(array(
                "/\[.*\]\d+[\/.*]/im", "/\[.*\]/im", "/jameson_read/i", "/\s{5,20}/ism", "/<br[^>]*?>/ism", "/&#\d+;/ism",
            ), array('', '', '', "\r\n", "\r\n", ''), $message);
            $tmpfile .= "\r\n" . trim($value['subject']) . "\r\n";
            $tmpfile .= "\r\n" . trim(strip_tags($tmp)) . "\r\n";
        }
        header("Content-Type: application/force-download");
        header("Content-Disposition: attachment; filename=" . preg_replace(array("/[\?\*\{\}\<\>&;\+\/\\\\]/"), array(''), $bookinfo['book_name']) . '.txt');
        echo $tmpfile;
    }
}
